// created by noellynch
// Jun 4, 2009

package com.sceneworker.editors.glslshadereditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.myjavautils.GenericUtils;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;

public class ExportShaderPanel extends JPanel {
	private		File		mcl_saveToDir = null;
	
	public ExportShaderPanel(final JDialog cl_parDlg, final String str_vert, final String str_frag) {
		setLayout(new MigLayout());
		
		final	JLabel		lcl_locationLabel = SwingComponentFactory.makeALabel("");
		
		JButton		lcl_locationToSaveToButton = SwingComponentFactory.makeAButton("Location To Save To");
		lcl_locationToSaveToButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File		lcl_file = GenericUtils.getAFile(null, true, true);
				
				if(lcl_file == null) { return; }
				
				mcl_saveToDir = lcl_file;
				lcl_locationLabel.setText(mcl_saveToDir.getName());
			}
		});
		
		JLabel			lcl_shaderNameLabel = SwingComponentFactory.makeALabel("Shader Name");
		final	JTextField		lcl_shadernameTF = SwingComponentFactory.makeATextField();
		
		JButton		lcl_cancel = SwingComponentFactory.makeAButton("Cancel");
		lcl_cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl_parDlg.setVisible(false);
			}
		});
		
		JButton		lcl_save = SwingComponentFactory.makeAButton("Save");
		lcl_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(mcl_saveToDir == null) {
					SimpleAlert.doAlert("You have not selected a directory to save to.", null);
					return;
				}
				
				if(lcl_shadernameTF.getText().equals("")) {
					SimpleAlert.doAlert("You have not given the shader pair a name.", null);
					return;
				}
				
				if(!saveToFile(lcl_shadernameTF.getText() + ".vert", mcl_saveToDir, str_vert)) {
					SimpleAlert.doAlert("File already exists.", null);
					return;
				}
				
				if(!saveToFile(lcl_shadernameTF.getText() + ".frag", mcl_saveToDir, str_frag)) {
					SimpleAlert.doAlert("File already exists.", null);
					return;
				}
				
				SimpleAlert.doAlert("Shaders saved.", null);
				
				cl_parDlg.setVisible(false);
			}
		});
		
		add(lcl_shaderNameLabel, "align right");
		add(lcl_shadernameTF, "wrap");
		add(lcl_locationToSaveToButton);
		add(lcl_locationLabel, "span, growx, wrap");
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_save, lcl_cancel);
	}
	
	private		boolean	saveToFile(String str_newFileName, File cl_dir, String str_s) {
		File	lcl_f = new File(cl_dir.getPath() + File.separator + str_newFileName);
		
		if(lcl_f.exists()) {
			return false;
		}
		
		try {
			FileWriter		lcl_out = new FileWriter(lcl_f);
			
			lcl_out.write(str_s);
			
			lcl_out.flush();
			lcl_out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return true;
	}

}
